<?php 
require_once("../global.inc.php");

class Statics extends CommonFrameWork{
	function pv($daturl,$user_time){
		$fileurl = $daturl."/pv.dat";
		$garray = file($fileurl);
		$num = explode("|",$garray[0]);
		$num[$user_time]=$num[$user_time]+1;
		if(filesize($fileurl)>40){
			$num = implode("|", $num);
			$fp = fopen($fileurl,"w");
			flock($fp,LOCK_EX);
			fwrite($fp,$num);
			flock($fp,LOCK_UN);
			fclose($fp);
		}
	}

	function ip($daturl,$user_time){
		$fileurl=$daturl."/ip.dat";
		$garray = file($fileurl);
		$num = explode("|",$garray[0]);
		$num[$user_time]=$num[$user_time]+1;
		if(filesize($fileurl)>40){
			$num = implode("|", $num);
			$fp = fopen($fileurl,"w");
			flock($fp,LOCK_EX);
			fwrite($fp,$num);
			flock($fp,LOCK_UN);
			fclose($fp);
		}
	}

	function from($daturl,$user_time2,$user_ip,$filename2,$user_ad){
		$fileurl=$daturl."/from.dat";
		$user_xi=array($user_time2,$user_ip,$filename2,$user_ad);
		$user_xi = implode("|", $user_xi)."|\r\n";
		$garray = file($fileurl);
		$fp=@fopen($fileurl,"w");
		flock($fp,LOCK_EX);
		@fwrite($fp,$user_xi);
		for($i=0; $i<49; $i++){
			@fwrite($fp,$garray[$i]);
		}
		flock($fp,LOCK_UN);
		@fclose($fp);
	}

	function pvip($daturl){
		$fileurl1 = $daturl."/pv.dat";
		$fileurl2 = $daturl."/ip.dat";
		$fileurl3 = $daturl."/pvip.dat";
		$garray1 = file($fileurl1);
		$garray2 = file($fileurl2);
		$garray3 = file($fileurl3);
		$num1 = explode("|", $garray1[0]);
		$num1a = explode("|", $garray3[0]);
		$num2 = explode("|", $garray2[0]);
		$num2a = explode("|", $garray3[1]);
		for($i=0; $i<24; $i++){
			$num1[$i] = $num1[$i] + $num1a[$i];
			$num2[$i] = $num2[$i] + $num2a[$i];
		}
		$num1 = implode("|", $num1);
		$num2 = implode("|", $num2);
		$fp = fopen($fileurl3,"w");
		flock($fp, LOCK_EX);
		fwrite($fp, $num1);
		fwrite($fp, $num2);
		flock($fp, LOCK_UN);
		fclose($fp);
	}

	function pvip_month($daturl,$pvdaynum,$ipdaynum,$user_today){
		$fileurl=$daturl."/pvip_mon.dat";
		$garray = file($fileurl);
		$num1 = explode("|",$garray[0]);
		$num2 = explode("|",$garray[1]);
		$num3 = explode("|",$garray[2]);
		$num4 = explode("|",$garray[3]);
		$num1[$user_today]=$num1[$user_today]+$pvdaynum;
		$num2[$user_today]=$num2[$user_today]+$ipdaynum;
		$num3[$user_today]=$num3[$user_today]+$pvdaynum;
		$num4[$user_today]=$num4[$user_today]+$ipdaynum;
		$num1 = implode("|", $num1);
		$num2 = implode("|", $num2);
		$num3 = implode("|", $num3);
		$num4 = implode("|", $num4);
		$fp = fopen($fileurl,"w");
		flock($fp,LOCK_EX);
		fwrite($fp,$num1);
		fwrite($fp,$num2);
		fwrite($fp,$num3);
		fwrite($fp,$num4);
		flock($fp,LOCK_UN);
		fclose($fp);
	}

	function pvip_year($daturl,$pvdaynum,$ipdaynum,$user_month){
		$fileurl=$daturl."/pvip_ye.dat";
		$garray = file($fileurl);
		$num1 = explode("|",$garray[0]);
		$num2 = explode("|",$garray[1]);
		$num3 = explode("|",$garray[2]);
		$num4 = explode("|",$garray[3]);
		$num1[$user_month]=$num1[$user_month]+$pvdaynum;
		$num2[$user_month]=$num2[$user_month]+$ipdaynum;
		$num3[$user_month]=$num3[$user_month]+$pvdaynum;
		$num4[$user_month]=$num4[$user_month]+$ipdaynum;
		$num1 = implode("|", $num1);
		$num2 = implode("|", $num2);
		$num3 = implode("|", $num3);
		$num4 = implode("|", $num4);
		$fp = fopen($fileurl,"w");
		flock($fp,LOCK_EX);
		fwrite($fp,$num1);
		fwrite($fp,$num2);
		fwrite($fp,$num3);
		fwrite($fp,$num4);
		flock($fp,LOCK_UN);
		fclose($fp);
	}

	function filego($daturl,$filename2){
		$filego=$filename2;
		$fileurl=$daturl."/file_go.dat";
		$fromyn="";
		$garray = file($fileurl);
		$cog=count($garray);
		for($i=0; $i<$cog; $i++) {
			$larray = explode("|",$garray[$i]);
			if($filego==$larray[1]){
				$larray[0]=$larray[0]+1;
				$larray[0]=sprintf("%08d",$larray[0]);
				$garray[$i] = implode("|",$larray);
				$fromyn="1";
			}
		}
		if($fromyn=="1"){
			$list=implode("",$garray);
			$fp = fopen($fileurl,"w");
			flock($fp,LOCK_EX);
			fwrite($fp,$list);
			flock($fp,LOCK_UN);
			fclose($fp);
		}
		if($fromyn==""){
			$num="00000001";
			$filego=array($num,$filego);
			$larray = implode("|",$filego)."|\n";
			$fp = fopen($fileurl,"a+");
			fwrite($fp,$larray);
			fclose($fp);
		}
	}

	function filefm($daturl,$user_ad){
		$filefm = str_replace("http://", "", $user_ad);
		$filefm = explode("/",$filefm);
		$filefm = $filefm[0];
		$fileurl=$daturl."/file_fm.dat";
		$fp = fopen($fileurl,"r");
		$garray = fread($fp,filesize($fileurl));
		fclose($fp);
		$fromyn="";
		$fromyn2="";
		if(ereg($filefm,$garray)){$fromyn="1";}else{$fromyn="";}
		if($fromyn=="1"){
			$garray = file($fileurl);
			$cog=count($garray);
			for($i=0; $i<$cog; $i++) {
				$larray = explode("|",$garray[$i]);
				if($filefm==$larray[1]){
					$larray[0]=$larray[0]+1;
					$larray[0]=sprintf("%08d",$larray[0]);
					$garray[$i] = implode("|",$larray);
					$fromyn2="1";
				}
			}
			$list=implode("",$garray);
			$fp = fopen($fileurl,"w");
			flock($fp,LOCK_EX);
			fwrite($fp,$list);
			flock($fp,LOCK_UN);
			fclose($fp);
		}
		if($fromyn=="" or $fromyn2==""){
			$num="00000001";
			$filefm=array($num,$filefm,$user_ad);
			$larray = implode("|",$filefm)."|\n";
			$fp = fopen($fileurl,"a+");
			fwrite($fp,$larray);
			fclose($fp);
		}
	}

	function newday($daturl){
		$fileurl=$daturl."/day_why.dat";
		$day=date("j");
		$fp = fopen($fileurl,"w");
		fwrite($fp,$day);
		fclose($fp);
	}

	function newmonth($daturl){
		$fileurl=$daturl."/day_mon.dat";
		$day=date("n");
		$fp = fopen($fileurl,"w");
		fwrite($fp,$day);
		fclose($fp);
	}

	function newyear($daturl){
		$fileurl=$daturl."/day_ye.dat";
		$day=date("Y");
		$fp = fopen($fileurl,"w");
		fwrite($fp,$day);
		fclose($fp);
	}

	function top_day($daturl,$pvdaynum,$ipdaynum){
		$fileurl=$daturl."/day_top.dat";
		$filegarray = file($fileurl);
		$garray = explode("|",$filegarray[0]);
		if($pvdaynum>$garray[0]){$garray[0]=$pvdaynum;}
		if($ipdaynum>$garray[1]){$garray[1]=$ipdaynum;}
		$listnum=implode("|",$garray);
		$fp = fopen($fileurl,"w");
		fwrite($fp,$listnum);
		fclose($fp);
	}

	function copy_today($daturl){
		$fileurl1=$daturl."/file_fm.dat";
		$fileurl1a=$daturl."/file_fma.dat";
		$fileurl2=$daturl."/file_go.dat";
		$fileurl2a=$daturl."/file_goa.dat";
		$fileurl3=$daturl."/file_key.dat";
		$fileurl3a=$daturl."/file_keya.dat";
		copy ($fileurl1,$fileurl1a);
		copy ($fileurl2,$fileurl2a);
		copy ($fileurl3,$fileurl3a);
	}

	function top10($file1,$file2){
		$garray1 = file($file1);
		rsort($garray1);
		$fp1 = fopen($file2,"r");
		$concent = fread($fp1,filesize($file2));
		fclose($fp1);

		for($i=0; $i<10; $i++) {
			$larray = explode("|",$garray1[$i]);
			if(!ereg($larray[1],$concent)){
				$fp = fopen($file2,"a+");
				fwrite($fp,$garray1[$i]);
				fclose($fp);
			}

			else{
				$garray2 = file($file2);
				$cog=count($garray2);
				for($j=0; $j<$cog; $j++) {
					$larray2 = explode("|",$garray2[$j]);
					if($larray[1]==$larray2[1]){
						if($larray[0]>$larray2[0]){
							$larray2[0]=$larray[0];
							$garray2[$j] = implode("|",$larray2);
						}
					}
				}
				$list2=implode("",$garray2);
				$fp2 = fopen($file2,"w");
				fwrite($fp2,$list2);
				fclose($fp2);
			}
		}
		$garray3 = file($file2);
		rsort($garray3);
		$fp3 = fopen($file2,"w+");
		for($m=0; $m<10; $m++) {
			fwrite($fp3,$garray3[$m]);
		}
		fclose($fp3);
	}

	function ippass($daturl,$user_ip){
		$fileurl=$daturl."/ip_all.dat";
		$fp = fopen($fileurl,"r");
		$garray = fread($fp,filesize($fileurl));
		fclose($fp);
		$ippass1="";
		$ippass2="";
		if(ereg($user_ip,$garray)){$ippass1="1";}
		if($ippass1==""){
			$user_ip2 = $user_ip."\r\n";
			$fp = fopen($fileurl,"a+");
			fwrite($fp,$user_ip2);
			fclose($fp);
			$ippass2="1";
		}
		return $ippass2;
	}

	function pvall($daturl){
		$fileurl=$daturl."/pv.dat";
		$garray = file($fileurl);
		$larray = explode("|",$garray[0]);
		for($i=0; $i<24; $i++){
			$pvnum=$pvnum+$larray[$i];
		}
		return $pvnum;
	}

	function ipall($daturl){
		$fileurl=$daturl."/ip.dat";
		$garray = file($fileurl);
		$larray = explode("|",$garray[0]);
		for($i=0; $i<24; $i++){
			$ipnum=$ipnum+$larray[$i];
		}
		return $ipnum;
	}

	function pvnew($daturl){
		$fileurl=$daturl."/pv.dat";
		$num="0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|"."\r\n";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function ipnew($daturl){
		$fileurl=$daturl."/ip.dat";
		$num="0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|"."\r\n";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function ipallnew($daturl){
		$fileurl=$daturl."/ip_all.dat";
		$num="";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function fromnew($daturl){
		$fileurl=$daturl."/file_fm.dat";
		$num="";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function fromnew2($daturl){
		$fileurl=$daturl."/from.dat";
		$num="";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function gonew($daturl){
		$fileurl=$daturl."/file_go.dat";
		$num="";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function keynew($daturl){
		$fileurl=$daturl."/file_key.dat";
		$num="";
		$fp = fopen($fileurl,"w+");
		fwrite($fp,$num);
		fclose($fp);
	}

	function todayadd($daturl){
		$fileurl=$daturl."/day2.dat";
		$fp    = fopen($fileurl,"r+");
		$number = fgets($fp,8);
		$number +=1;
		rewind($fp);
		fputs($fp,$number);
		fclose($fp);
	}

	function monthnew($daturl){
		$fileurl=$daturl."/pvip_mon.dat";
		$garray = file($fileurl);
		$num="0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|"."\r\n";
		$fp = fopen($fileurl,"w+");

		fwrite($fp,$num);
		fwrite($fp,$num);
		fwrite($fp,$garray[2]);
		fwrite($fp,$garray[3]);
		fclose($fp);
	}

	function yearnew($daturl){
		$fileurl=$daturl."/pvip_ye.dat";
		$garray = file($fileurl);
		$num="0|0|0|0|0|0|0|0|0|0|0|0|0|"."\r\n";
		$fp = fopen($fileurl,"w+");

		fwrite($fp,$num);
		fwrite($fp,$num);
		fwrite($fp,$garray[2]);
		fwrite($fp,$garray[3]);
		fclose($fp);
	}

	function forkey($user_ad){
		if(ereg(".google.",$user_ad)){$comefen="q=";$comepass=1;$comelei="UTF-8";}
		if(ereg(".yahoo.",$user_ad)){$comefen="p=";$comepass=1;$comelei="UTF-8";}
		if(ereg(".msn.",$user_ad)){$comefen="q=";$comepass=1;$comelei="UTF-8";}
		if(ereg(".alltheweb.",$user_ad)){$comefen="q=";$comepass=1;$comelei="UTF-8";}
		if(ereg(".baidu.",$user_ad)){$comefen="wd=";$comepass=1;}
		if(ereg(".3721.",$user_ad)){$comefen="p=";$comepass=1;}
		if(ereg(".sohu.",$user_ad)){$comefen="query=";$comepass=1;}
		if(ereg(".163.",$user_ad)){$comefen="q=";$comepass=1;}
		if(ereg(".sina.",$user_ad)){$comefen="searchkey=";$comepass=1;}
		if(ereg(".tom.",$user_ad)){$comefen="word=";$comepass=1;}
		if(ereg(".zhongsou.",$user_ad)){$comefen="word=";$comepass=1;}
		if(ereg(".yisou.",$user_ad)){$comefen="p=";$comepass=1;}
		if(ereg(".sogou.",$user_ad)){$comefen="query=";$comepass=1;}
		if(ereg(".qq.",$user_ad)){$comefen="word=";$comepass=1;}
		if(ereg(".onseek.",$user_ad)){$comefen="term=";$comepass=1;}
		if(ereg(".lycos.",$user_ad)){$comefen="query=";$comepass=1;}
		if(ereg(".aol.",$user_ad)){$comefen="query=";$comepass=1;}

		if($comepass=="1"){
			$comekey = explode($comefen,$user_ad);
			$comekey = explode("&",$comekey[1]);
			$comekey = explode("&",$comekey[0]);
			$comekey[0] = urldecode($comekey[0]);
			$s = $comekey[0];
			if($comelei != "UTF-8"){
				require_once("encoding.inc.php");
				$CharEncoding = new Encoding();
				$CharEncoding->SetGetEncoding("GBK");
				$CharEncoding->SetToEncoding("UTF-8");
				$s = $CharEncoding->EncodeString($s);
			}
		}
		if(ereg("%u",$s)){$s="";}
		return $s;
	}

	function keyadd($daturl,$s,$user_ad){
		$fileurl=$daturl."/file_key.dat";
		$fp = fopen($fileurl,"r");
		$garray = fread($fp,filesize($fileurl));
		fclose($fp);
		$fromyn="";
		$fromyn2="";
		if(ereg($s,$garray)){$fromyn="1";}else{$fromyn="";}
		if($fromyn=="1"){
			$garray = file($fileurl);
			$cog=count($garray);
			for($i=0; $i<$cog; $i++) {
				$larray = explode("|",$garray[$i]);
				if($s==$larray[1]){
					$larray[0]=$larray[0]+1;
					$larray[0]=sprintf("%08d",$larray[0]);
					$garray[$i] = implode("|",$larray);
					$fromyn2="1";
				}
			}
			$list=implode("",$garray);
			$fp = fopen($fileurl,"w");
			flock($fp,LOCK_EX);
			fwrite($fp,$list);
			flock($fp,LOCK_UN);
			fclose($fp);
		}
		if($fromyn=="" or $fromyn2==""){
			$num="1";
			$num=sprintf("%08d",$num);
			$filefm=array($num,$s,$user_ad);
			$larray = implode("|",$filefm)."|\n";
			$fp = fopen($fileurl,"a+");
			fwrite($fp,$larray);
			fclose($fp);
		}
	}

	function main(){
		$user_time = date("G");
		$user_time2 = date("G:i:s");
		$user_ip = getenv('REMOTE_ADDR');
		$user_ad = $_GET['user_ad'];
		$filename2 = $_GET['filename2'];
		if($user_ad == ""){
			$user_ad = $this->_configinfo[websit][site_url];
		}
		$daturl = "../".$this->_configinfo[stats][datapath];
		$fileurl = $daturl."/day_why.dat";
		$fday1 = fopen($fileurl,"r");
		$user_today = fgets($fday1,4);
		fclose($fday1);
		$fileurl2=$daturl."/day_mon.dat";
		$fday2 = fopen($fileurl2,"r");
		$user_month = fgets($fday2,4);
		fclose($fday2);
		$fileurl3=$daturl."/day_ye.dat";
		$fday3 = fopen($fileurl3, "r");
		$user_year = fgets($fday3, 6);
		fclose($fday3);
		$day = date("j");
		$month = date("n");
		$year = date("Y");
		if($user_today == $day){
			$dayok="0";
		}else{
			$dayok="1";
		}
		if($user_month == $month){
			$monthok="0";
		}else{
			$monthok="1";
		}
		if($user_year == $year){
			$yearok="0";
		}else{
			$yearok="1";
		}

		$this->pv($daturl,$user_time);
		$this->from($daturl,$user_time2,$user_ip,$filename2,$user_ad);
		$this->filego($daturl,$filename2);
		$this->filefm($daturl,$user_ad);
		$s=$this->forkey($user_ad);
		if($s!=""){
			$this->keyadd($daturl,$s,$user_ad);
		}
		$ippass2 = $this->ippass($daturl,$user_ip);
		if($ippass2=="1"){
			$this->ip($daturl,$user_time);
		}

		if($dayok=="1"){
			$this->pvip($daturl);
			$pvdaynum = $this->pvall($daturl);
			$ipdaynum = $this->ipall($daturl);
			$this->pvip_month($daturl,$pvdaynum,$ipdaynum,$user_today);
			$this->pvip_year($daturl,$pvdaynum,$ipdaynum,$user_month);
			$this->top_day($daturl,$pvdaynum,$ipdaynum);
			$this->newday($daturl);
			$this->pvnew($daturl);
			$this->ipnew($daturl);
			$this->todayadd($daturl);
			$this->ipallnew($daturl);
			$this->copy_today($daturl);
			$file1=$daturl."/file_fm.dat";
			$file2=$daturl."/fmtop10.dat";
			$this->top10($file1,$file2);
			$file1=$daturl."/file_go.dat";
			$file2=$daturl."/gotop10.dat";
			$this->top10($file1,$file2);
			$file1=$daturl."/file_key.dat";
			$file2=$daturl."/keytop10.dat";
			$this->top10($file1,$file2);
			$this->fromnew($daturl);
			$this->fromnew2($daturl);
			$this->gonew($daturl);
			$this->keynew($daturl);
			if($monthok=="1"){
				$this->monthnew($daturl);
				$this->newmonth($daturl);
			}
			if($yearok=="1"){
				$this->yearnew($daturl);
				$this->newyear($daturl);
			}
		}
	}

}

$statics = new Statics();
$statics->main();
unset($statics);

?>